// src/models/global.ts
import { useEffect, useState } from 'react';
import { getDicts } from '@/services/data';

export const useStatusDictsModel = () => {
  const [loading, setLoading] = useState<boolean>(true);
  const [statusOptions, setstatusOptions] = useState<any[]>([]);
  const loadUserInfo = async () => {
    setLoading(true);
    try {
      const res = await getDicts('sys_normal_disable');
      if (res.code === 200) {
        setstatusOptions(
          res.data?.map((item: { dictLabel: any; dictValue: any }) => {
            return {
              label: item.dictLabel,
              value: item.dictValue,
            };
          }),
        );
      }
    } catch (e) {
      console.log(e);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    // 页面初始化时加载
    loadUserInfo();
  }, []);

  return {
    statusOptions,
    loading,
  };
};

export const useSexDictsModel = () => {
  const [loading, setLoading] = useState<boolean>(true);
  const [statusOptions, setstatusOptions] = useState<any[]>([]);
  const loadUserInfo = async () => {
    setLoading(true);
    try {
      const res = await getDicts('sys_user_sex');
      if (res.code === 200) {
        setstatusOptions(
          res.data?.map((item: { dictLabel: any; dictValue: any }) => {
            return {
              label: item.dictLabel,
              value: item.dictValue,
            };
          }),
        );
      }
    } catch (e) {
      console.log(e);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    // 页面初始化时加载
    loadUserInfo();
  }, []);

  return {
    statusOptions,
    loading,
  };
};

// getSysShowHideDict
export const useSysShowHideDictsModel = () => {
  const [loading, setLoading] = useState<boolean>(true);
  const [showHideOptions, setShowHideOptions] = useState<any[]>([]);
  const loadUserInfo = async () => {
    setLoading(true);
    try {
      const res = await getDicts('sys_show_hide');
      if (res.code === 200) {
        setShowHideOptions(
          res.data?.map((item: { dictLabel: any; dictValue: any }) => {
            return {
              label: item.dictLabel,
              value: item.dictValue,
            };
          }),
        );
      }
    } catch (e) {
      console.log(e);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    // 页面初始化时加载
    loadUserInfo();
  }, []);

  return {
    showHideOptions,
    loading,
  };
};
